package yxk.task;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.db.DB;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.schedule.api.JobInfo;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.MetadataServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.schedule.ScheduleServiceHelper;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.sdk.plugin.Plugin;
import kd.wtc.wtbs.business.auth.HRAuthUtil;
import kd.wtc.wtbs.business.caltask.common.WTCTaskRequestStd;
import kd.wtc.wtbs.business.task.base.WTCDistributeTaskHelper;
import kd.wtc.wtbs.business.task.common.WTCSource;
import kd.wtc.wtbs.business.task.dispatch.WTCTaskDispatchRequest;
import kd.wtc.wtbs.business.web.file.AttFileQueryServiceImpl;
import kd.wtc.wtbs.common.model.attfile.AttFileQueryParam;
import kd.wtc.wtbs.common.util.WTCDateUtils;
import kd.wtc.wtpm.business.cardmatch.SignCardService;
import kd.wtc.wtte.business.tietask.TieTimingCommonHelper;
import kd.wtc.wtte.business.timedtask.TieTimingCurrentMonthTask;
import yxk.utils.DateUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lijiawang
 * @Date 2023/9/12 10:27
 * @Description:匹配上个月
 */
public class MatchresultTask extends AbstractTask implements Plugin {
    private static final Log LOG = LogFactory.getLog(MatchresultTask.class);

    //上个月的取卡匹配调度作业
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        Date today = new Date();
        Date expectStartDate = TieTimingCommonHelper.getPreMonthFirstDayZeroTime(today);
        Date expectEndDate = TieTimingCommonHelper.getPreMonthLastDayLastDate(today);
        Date zero = DateUtil.getZero(expectEndDate);
        startMatchScheduledTask(expectStartDate,zero);
    }

    public void startMatchScheduledTask(Date startDate,Date endDate) {
        LOG.info("取卡匹配定时任务开始执行,开始日期[{}]，结束日期[{}]", startDate, endDate);
        WTCTaskRequestStd request = new WTCTaskRequestStd();
        String version = CodeRuleServiceHelper.getNumber("wtpm_matchresult", BusinessDataServiceHelper.newDynamicObject("wtpm_matchresult"), (String)null);
        long taskId = this.saveInitTask(startDate, endDate, version);
        request.setTaskId(taskId);
        request.setVersion(version);
        request.setCategory("wtpm_match");
        request.setCreatorId(1L);
        request.setSource(WTCSource.SCHEDULE.code);
        request.setTrial(false);
        request.setStartDate(startDate);
        request.setEndDate(endDate);
        request.setAppId("wtpm");
        LOG.info("取卡匹配,查询考勤档案开始");
        AttFileQueryParam param = new AttFileQueryParam(startDate, endDate);
        param.setAuthCheck(Boolean.FALSE);
        param.setProperties(HRAuthUtil.ATT_FILE_SHARD_TASK_PROPERTY);
        List<DynamicObject> attFiles = AttFileQueryServiceImpl.getInstance().queryAttFiles(param);
        LOG.info("取卡匹配,查询考勤档案返回,size={}", attFiles.size());
        List<Map<String, Object>> detail = HRAuthUtil.getAuthFieldByAttFileDynBatch(attFiles);
        request.setDetail(detail);
        LOG.info("取卡匹配定时任务,考勤档案入参 size[{}]", detail.size());
        request.setDetail(detail);
        Map<String, Object> paramMap = new HashMap(4);
        paramMap.put("number", taskId);
        paramMap.put("name", ResManager.loadKDString("取卡匹配", "SignCardService_0", "wtc-wtpm-business", new Object[0]));
        request.setParams(paramMap);
        List<WTCTaskDispatchRequest> subTaskDispatchRequestList = WTCDistributeTaskHelper.shardingSaveAndSlimReqStd(request);
        JobInfo mainTaskExecutorJobInfo = WTCDistributeTaskHelper.getMainTaskExecutorJobInfo(request, subTaskDispatchRequestList);
        ScheduleServiceHelper.dispatch(mainTaskExecutorJobInfo);
    }

    private long saveInitTask(Date startDate, Date endDate, String version) {
        HRBaseServiceHelper serviceHelper = new HRBaseServiceHelper("wtpm_matchresult");
        Long id = DB.genGlobalLongId();
        DynamicObject result = new DynamicObject(MetadataServiceHelper.getDataEntityType("wtpm_matchresult"));
        result.set("id", id);
        result.set("task", id);
        result.set("org", 100000L);
        result.set("version", version);
        result.set("startdate", startDate);
        result.set("enddate", endDate);
        result.set("creator", RequestContext.get().getCurrUserId());
        result.set("createtime", new Date());
        DynamicObject resultDy = (DynamicObject)serviceHelper.saveOne(result);
        return resultDy.getLong("id");
    }
}